﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLACQUY.DTO;
using System.Data.OleDb;
using System.Windows.Forms;

namespace QLACQUY.DAO
{
    class CTPhieuGiaoHangDAO:DataAccess
    {
        public int themCTPhieu(CTPhieuGiaoHangDTO dto)
        {
            OleDbConnection cn = new OleDbConnection();
            cn = this.DataConnection();
            string strSQL = "insert into CHI_TIET_PHIEU_GIAO_HANG(MaSanPham, DonGia, SoLuong, ThanhTien, MaPhieuGiaoHang) values(?,?,?,?,?)";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@MaSanPham", OleDbType.Integer);
            cmd.Parameters["@MaSanPham"].Value = dto.MaSanPham;

            cmd.Parameters.Add("@DonGia", OleDbType.Double);
            cmd.Parameters["@DonGia"].Value = dto.DonGia;

            cmd.Parameters.Add("@SoLuong", OleDbType.Integer);
            cmd.Parameters["@SoLuong"].Value = dto.SoLuong;

            if (dto.ThanhTien == 0)
                dto.ThanhTien = dto.SoLuong * dto.DonGia;
            cmd.Parameters.Add("@ThanhTien", OleDbType.Double);
            cmd.Parameters["@ThanhTien"].Value = dto.ThanhTien;

            cmd.Parameters.Add("@MaPhieuGiaoHang", OleDbType.Integer);
            cmd.Parameters["@MaPhieuGiaoHang"].Value = dto.MaPhieuGiaoHang;

            try
            {
                cmd.ExecuteNonQuery();
                return 1;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return 0;
            }            
        }
    }
}
